package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishesService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class DishesServiceImpl implements DishesService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     */
    @Transactional
    public void dishesInsert(DishDTO dishDTO) {

        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.dishesInsert(dish);

        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.tasteInsert(flavors);
        }
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Override
    public void dishUpdate(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //修改菜品
        dishMapper.dishUpdate(dish);

        //先删除口味数据
        dishFlavorMapper.dishFlavorDelete(dishDTO.getId());
        //在添加口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.tasteInsert(flavors);
        }
        //dishFlavorMapper.tasteUpdate(dishFlavor);
    }

    /**
     * 分页查询
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public DishVO queryById(Long id) {

        DishVO dishVO = dishMapper.queryById(id);

        return dishVO;
    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryList(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVO = dishMapper.queryList(dishPageQueryDTO);

        return new PageResult(dishVO.getTotal(), dishVO.getResult());
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Override
    @Transactional //开启事务
    public void dishDelete(List<Long> ids) {

        //判断当前菜品是否能够删除 -- 是否存在起售中的菜品
        for (Long id :ids) {
            Dish byId = dishMapper.getById(id);
            if (byId.getStatus() == StatusConstant.ENABLE){
                //当前菜品起售中不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断当前菜品是否能够删除 -- 是否是被关联套餐的菜品
        List<Long> setmealById = setmealDishMapper.getSetmealById(ids);
        if (setmealById != null && setmealById.size() > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
/*
        方式1 不推荐
        for (Long aLong : ids) {
            //删除菜品表中的数据
            dishMapper.dishDelete(aLong);
            //删除口味表中的数据
            dishFlavorMapper.dishFlavorDelete(aLong);
        }*/

        //删除菜品表中的数据
        dishMapper.dishDeleteList(ids);
        //删除口味表中的数据
        dishFlavorMapper.dishFlavorDeleteList(ids);
    }

    @Override
    public List<Dish> listById(Long id) {

        List<Dish> listById= dishMapper.listById(id);
        return listById;
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {

            //根据菜品id查询对应的口味
        List<DishVO> dishVOList = dishMapper.getByDishId(dish);

        return dishVOList;
    }
}
